package com.llkj.qingnianguoxue.adapter;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.HotRankingBean;
import com.llkj.qingnianguoxue.util.ClickViewCallback;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.List;

/**
 * 购物车
 */
public class ShopCartAdapter extends BaseQuickAdapter<HotRankingBean.RankingBean, BaseViewHolder> {
    private ClickViewCallback listener;

    public ShopCartAdapter(int layoutResId, List<HotRankingBean.RankingBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, HotRankingBean.RankingBean item) {
        ImageView ivPic = baseViewHolder.getView(R.id.pic);
        FrameLayout.LayoutParams picParam = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
        picParam.height = DisplayUtil.dip2px(mContext,85);

        baseViewHolder.setText(R.id.tv_title, item.getTitle());
        baseViewHolder.setText(R.id.tv_introduction,item.getAbstractX());
        baseViewHolder.setText(R.id.tv_price,"￥"+item.getPrice());
        TextView tvTag = baseViewHolder.getView(R.id.tvTag);
        if(!StringUtils.isEmpty(item.getClassify())){
            tvTag.setVisibility(View.VISIBLE);
            tvTag.setText(item.getClassify());
        }else{
            tvTag.setVisibility(View.INVISIBLE);
        }

        ImageView ivCheck = baseViewHolder.getView(R.id.iv_check);
        if (item.isCheck()) {
            ivCheck.setSelected(true);
        } else {
            ivCheck.setSelected(false);
        }
//        setClickView(ivCheck, baseViewHolder.getLayoutPosition(), 0);
        baseViewHolder.addOnClickListener(R.id.iv_check);

        ImageView ivPlay = baseViewHolder.getView(R.id.iv_video_play);
        TextView tvVideoTime = baseViewHolder.getView(R.id.tv_video_time);
        if(2==item.getType()){// 类型 1 视频2 图书
            picParam.width = picParam.height*3/4;
            ivPlay.setVisibility(View.INVISIBLE);
            tvVideoTime.setVisibility(View.INVISIBLE);
        }else{
            picParam.width = picParam.height*16/9;
            ivPlay.setVisibility(View.VISIBLE);
            tvVideoTime.setVisibility(View.VISIBLE);
            tvVideoTime.setText(item.getDuration());
        }

        SwipeRevealLayout swipeDelGroup = baseViewHolder.getView(R.id.swipeDelGroup);
        TextView tvDelItem = baseViewHolder.getView(R.id.tvDelItem);
        tvDelItem.getLayoutParams().height = picParam.height+DisplayUtil.dip2px(mContext, 20);
        baseViewHolder.addOnClickListener(R.id.tvDelItem);
        swipeDelGroup.close(true);
        swipeDelGroup.setSwipeListener(new SwipeRevealLayout.SimpleSwipeListener() {
            @Override
            public void onClosed(SwipeRevealLayout view) {
                super.onClosed(view);
            }

            @Override
            public void onOpened(SwipeRevealLayout view) {
                super.onOpened(view);
            }
        });
        ImageLoadingUtil.loadImgGlideRoundCenterCrop(ivPic, item.getImg());
    }

    private void setClickView(View v, final int position, final int type) {
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.clickViewCallback(position, type, v);
                }
            }
        });
    }
}
